//
//  SearchViewController.m
//  Store041
//
//  Created by amdin on 14-7-24.
//  Copyright (c) 2014年 panquan/liang. All rights reserved.
//

#import "CaseListViewController.h"
#import "ClothSupply.h"
#import "UIButton+WebCache.h"
#import "HAODEsingleton.h"
#import "HAODEsingleton.h"
#import "UIViewExt.h"
#import "HttpRequest.h"
#import "JSONKit.h"
#import "addhuiyuanViewController.h"
#import "CouponCell.h"
#import "RDVTabBarController.h"
#import "MJRefresh.h"
#import "UIButton+WebCache.h"
#import "GetProductListViewController.h"
#import "addCaseViewController.h"
#import "CaseDetailViewController.h"
#import "sxview.h"
#import "ServiceListCell.h"
@interface CaseListViewController ()<UISearchBarDelegate,UIScrollViewDelegate,UITextFieldDelegate,UINavigationControllerDelegate,UIGestureRecognizerDelegate,UITableViewDataSource,UITableViewDelegate>
{
    HAODEsingleton *commdity;
    UIScrollView *scrollView;
    UISearchBar *search;
    int count;
    NSString*TotalCount;
    CGFloat height;
    UIButton*sat;
    UIButton*sat1;
    UIButton*btn2;
    NSArray *array_hot;
    UILabel *labelLoad;
      UILabel *labelLoad1;
    UIActivityIndicatorView *activity;
    UIButton* sxBtn;
    NSMutableArray*typeName;
    NSMutableArray*typeCode;
    NSMutableArray*isSingle;
    NSMutableArray*congzhicurrent;
    NSMutableArray*current;
    NSMutableArray*dataType;
    NSMutableArray *searchBeans;
    NSMutableArray*itemValue;
    NSMutableArray*itemName;
    NSMutableArray*items;
    UITextField*openTimeTextFied;
    UITextField*closeTimeTextFied;
    sxview*daogouView;
    UIView*view;
    UITableView *table_list;
    UIScrollView*mainScroll;
    NSMutableArray* arrrss;
    UIButton*shaixuanBtn;
    bool saveAccount1;
    
}
@property(nonatomic,strong)NSString* sta;
@property(nonatomic,retain)UIDatePicker* da;
@end

@implementation CaseListViewController
@synthesize alt = alt;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
    
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    
    
    
    self.navigationController.delegate = nil;
    
}
-(void)addProduct:(UIButton*)sender{
     commdity.gengxin=@"youshuju";
    commdity.content=nil;
    commdity.caseStylecontent=nil;
    commdity.caseHousecontent=nil;
    
    commdity.code=nil;
    commdity.caseHouseCode=nil;
    commdity.caseStyleCode=nil;
    addCaseViewController *detailViewController = [[addCaseViewController alloc] init];
    [self.navigationController pushViewController:detailViewController animated:YES];
    
    
}


-(void)sat:(UIButton*)sender{
    [sat1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [sat setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    searchBeans=nil;
    self.sta=@"N";
    [self setup];
}
-(void)sat1:(UIButton*)sender{
    
    self.sta=@"H";
    [self setup];
    [sat setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [sat1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [[self rdv_tabBarController] setTabBarHidden:YES];
    
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    
    self.title=@"全部案例";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor blackColor],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    

   


     self.sta=@"N";
    commdity = [HAODEsingleton shared];
    
    
    
    if ([commdity.gengxin isEqualToString:@""]) {
         [self setup];
    }else{
    
    
    
    }
   


}


- (void)viewDidLoad
{
    
    [super viewDidLoad];
    saveAccount1=NO;
    typeCode=[[NSMutableArray alloc]init];
    typeName=[[NSMutableArray alloc]init];
    items=[[NSMutableArray alloc]init];
    isSingle=[[NSMutableArray alloc]init];
    current=[[NSMutableArray alloc]init];
    congzhicurrent=[[NSMutableArray alloc]init];
    dataType=[[NSMutableArray alloc]init];
    commdity=[HAODEsingleton shared];
    commdity.opentime=nil;
    commdity.closeTime=nil;
    commdity.content3=@"";
        
    UIButton*tianjiasxBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0,  60/2, 60/2)];
    [tianjiasxBtn setImage:[UIImage imageNamed:@"tianjia"] forState:UIControlStateNormal];
    [tianjiasxBtn addTarget:self action:@selector(addProduct:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* myBtnBtn = [[UIBarButtonItem alloc] initWithCustomView:tianjiasxBtn];
    self.navigationItem.rightBarButtonItem = myBtnBtn;
    UIView*view11=[[UIView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth,80/2)];
    view11.backgroundColor= zuiqianhuicolor;
    [self.view addSubview:view11];
    
    
    
    sat=[[UIButton alloc]initWithFrame:CGRectMake(-20, 0, ScreenWidth/3, 40)];
    [sat setTitle:@"最新" forState:UIControlStateNormal];
    [sat setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    sat.titleLabel.textAlignment=UIControlContentHorizontalAlignmentLeft;
    sat.titleLabel.font=[UIFont systemFontOfSize:16];
    [sat addTarget:self action:@selector(sat:) forControlEvents:UIControlEventTouchUpInside];
    [view11 addSubview:sat];
    
    sat1=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/3, 0, ScreenWidth/3, 40)];
    [sat1 setTitle:@"热度" forState:UIControlStateNormal];
    [sat1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    sat1.titleLabel.font=[UIFont systemFontOfSize:16];
    [sat1 addTarget:self action:@selector(sat1:) forControlEvents:UIControlEventTouchUpInside];
    sat1.titleLabel.textAlignment=NSTextAlignmentCenter;
    [view11 addSubview:sat1];
    
    
    
    shaixuanBtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth*590/750,  15/2, 70,30)];
    [shaixuanBtn.layer setCornerRadius:2.0];
    [shaixuanBtn.layer setBorderWidth:0];
    [shaixuanBtn.layer setMasksToBounds:YES];
    [shaixuanBtn addTarget:self action:@selector(sxuan:) forControlEvents:UIControlEventTouchUpInside];
    CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref2 = CGColorCreate(colorSpace2,(CGFloat[]){153/255.0,153/255.0, 153/255.0, 1.0   });
    [shaixuanBtn.layer setBorderColor:colorref2];//边框颜色
    [shaixuanBtn setTitle:@"     筛选" forState:UIControlStateNormal];
    shaixuanBtn.titleLabel.font=[UIFont systemFontOfSize:16];
    [shaixuanBtn setTitleColor:shenhuicolor forState:UIControlStateNormal];
    shaixuanBtn.titleLabel.textAlignment=NSTextAlignmentRight;
    [view11 addSubview:shaixuanBtn];
    
    sxBtn = [[UIButton alloc] initWithFrame:CGRectMake(3, 0, 30, 30)];
    [sxBtn setImage:[UIImage imageNamed:@"shaixuan"] forState:UIControlStateNormal];
    [sxBtn addTarget:self action:@selector(sxuan:) forControlEvents:UIControlEventTouchUpInside];
    [shaixuanBtn addSubview:sxBtn];
    
    
    
    
    
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    if (Token ==nil) {
        Token=@"dd";
    }
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:@"case" forKey:@"filterType"];
    
    NSLog(@"====%@",param);
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    [HttpRequest requestData:@"Common/AdvancedFilterList" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            
            NSLog(@"====－－－－－－－%@",severDataDict.dataDict);
            
            
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                
                NSMutableArray* arrr=[[NSMutableArray alloc]init];
                
                arrr=[severDataDict.dataDict objectForKey:@"DataLine"];
                
                for (NSDictionary* dicc in arrr )
                {
                    [typeName addObject:[dicc valueForKey:@"typeName"]];
                    [typeCode addObject:[dicc valueForKey:@"typeCode"]];
                    [items addObject:[dicc valueForKey:@"items"]];
                    [isSingle addObject:[dicc valueForKey:@"isSingle"]];
                    
                    [congzhicurrent addObject:[dicc valueForKey:@"current"]];
                    [current addObject:[dicc valueForKey:@"current"]];
                    [dataType addObject:[dicc valueForKey:@"dataType"]];
                    
                    for (NSDictionary* dicc1 in items )
                    {
                        [itemName addObject:[dicc1 valueForKey:@"itemName"]];
                        [itemValue addObject:[dicc1 valueForKey:@"itemValue"]];
                        
                    }
                    
                    
                }
                
                
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.view];
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];
    
    
}

    
- (void)setup{
    count = 1;
    [scrollView removeFromSuperview];
    
    scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 80/2+64, ScreenWidth, ScreenHeight-64)];
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    

    for (UIView *view1 in [scrollView subviews]) {
        [view1 removeFromSuperview];
    }
    
    [search resignFirstResponder];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    if (Token ==nil) {
        Token=@"dd";
    }
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue: self.sta forKey:@"dataType"];
    [param setValue: @"30" forKey:@"pageSize"];
    [param setValue: @"1" forKey:@"pageIndex"];
    [param setValue:searchBeans forKey:@"searchBeans"];
    NSLog(@"====%@",param);
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:20 view:self.view];
    
    [activity stopAnimating];
    [HttpRequest requestData:@"Business/Case/List" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            if (searchBeans.count>0) {
                
               UIImage *img=[UIImage imageNamed:@"shaixuan_hight"];
                UIImage *tinted = [img rt_tintedImageWithColor:luseColor];
                [shaixuanBtn setTitleColor:luseColor forState:UIControlStateNormal];
                [sxBtn setImage:tinted forState:UIControlStateNormal];                
                
            }else{
                [shaixuanBtn setTitleColor:shenhuicolor forState:UIControlStateNormal];
                [sxBtn setImage:[UIImage imageNamed:@"shaixuan"] forState:UIControlStateNormal];
            }
            

            NSLog(@"====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                
                 TotalCount=[severDataDict.dataDict objectForKey:@"TotalCount"];
                
                array_hot = [severDataDict.dataDict objectForKey:@"DataLine"];
                if (![array_hot isEqual:[NSNull null]]) {
                    
                    for (int i = 0,j = -1; i < array_hot.count; i++) {
                        if (i%2 == 0) {
                            j++;
                        }
                        ClothSupply *clothView = [[ClothSupply alloc]initWithFrame:CGRectMake(10+(ScreenWidth/2-5)*(i%2), 10+210*j, ScreenWidth/2-15, 200)];
                        
                        clothView.label_name.text =[NSString stringWithFormat:@"%@", [[array_hot objectAtIndex:i] objectForKey:@"title"]];
                        
                        [clothView.buttonView setImageWithURL:[NSURL URLWithString:[[[[array_hot objectAtIndex:i] objectForKey:@"imageList"]objectAtIndex:0]objectForKey:@"imageUrlFull"]] placeholderImage:[UIImage imageNamed:@""]];
                        [clothView.buttonView setTitle:[[array_hot objectAtIndex:i] objectForKey:@"caseID"] forState:UIControlStateNormal];
                         [clothView.buttonView.imageView setContentMode:UIViewContentModeScaleAspectFill];
                        [clothView.buttonView setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
                        [clothView.buttonView addTarget:self action:@selector(commodity:) forControlEvents:UIControlEventTouchUpInside];
                        
                        [scrollView addSubview:clothView];
                        
                        height = 210*(array_hot.count/2+array_hot.count%2);
                    }
                }
                
            
                
                scrollView.contentSize = CGSizeMake(ScreenWidth, height+64);
                
                
                labelLoad1 = [[UILabel alloc]init];
                labelLoad1.text = @"下拉加载新数据";
                labelLoad1.textColor=zhonghuiColor;
                labelLoad1.textAlignment = NSTextAlignmentCenter;
                [scrollView addSubview:labelLoad1];
                labelLoad1.frame = CGRectMake(0, -150, ScreenWidth, 50);
                
                
                activity = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(80, 10, 30, 30)];
                activity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
                [labelLoad1 addSubview:activity];
                
                
                
                labelLoad = [[UILabel alloc]init];
                labelLoad.text = @"上拉加载更多";
                labelLoad.textAlignment = NSTextAlignmentCenter;
                [scrollView addSubview:labelLoad];
                 labelLoad.textColor=zhonghuiColor;
                labelLoad.frame = CGRectMake(0, scrollView.contentSize.height+150, ScreenWidth, 50);
                
                
                activity = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(80, 10, 30, 30)];
                activity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
                [labelLoad addSubview:activity];
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.view];
            }
        
        
        
        }
        
        
        
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];

    
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    
}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView1 willDecelerate:(BOOL)decelerate
{
    if (scrollView1.contentOffset.y < -150) {
        labelLoad1.text = @"正在加载。。。";
        [activity startAnimating];
      self.sta=@"N";
        searchBeans=nil;
       
      [self setup];
    
    
    }
    if (scrollView1.contentOffset.y > scrollView1.contentSize.height-scrollView1.height+100) {
        labelLoad.text = @"正在加载。。。";
        [activity startAnimating];
        if (TotalCount.intValue/30>=count) {
            count++;
       
 
        
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString* Token = [user objectForKey:@"tokenId"];
        if (Token ==nil) {
            Token=@"dd";
        }
        NSMutableDictionary *param = [NSMutableDictionary new];
        [param setValue:Token forKey:@"tokenId"];
        [param setValue: @"N" forKey:@"dataType"];
        [param setValue: @"30" forKey:@"pageSize"];
        [param setValue:searchBeans forKey:@"searchBeans"];
          [param setValue:[NSString stringWithFormat:@"%d" ,count] forKey:@"pageIndex"];
        
        NSLog(@"====%@",param);
        [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:20 view:self.view];
        
        
        [HttpRequest requestData:@"Business/Case/List" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                
                NSLog(@"====%@",severDataDict.dataDict);
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    [Global hideProgressHUDInternal:self.view];
            NSArray *array_hot2 =[severDataDict.dataDict objectForKey:@"DataLine"];
            
            if (![array_hot2 isEqual:[NSNull null]]) {
                
                
                for (int i = 0,j = -1; i < array_hot2.count; i++) {
                    if (i%2 == 0) {
                        j++;
                    }
                      ClothSupply *clothView = [[ClothSupply alloc]initWithFrame:CGRectMake(10+(ScreenWidth/2-5)*(i%2), 10+210*j+height, ScreenWidth/2-15, 200)];

                    clothView.label_name.text =[NSString stringWithFormat:@"%@", [[array_hot objectAtIndex:i] objectForKey:@"title"]];
                    
                    [clothView.buttonView setImageWithURL:[NSURL URLWithString:[[[[array_hot2 objectAtIndex:i] objectForKey:@"imageList"]objectAtIndex:0]objectForKey:@"imageUrlFull"]] placeholderImage:[UIImage imageNamed:@""]];
                   [clothView.buttonView setTitle:[[array_hot objectAtIndex:i] objectForKey:@"caseID"] forState:UIControlStateNormal];
                    [clothView.buttonView setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
                  [clothView.buttonView addTarget:self action:@selector(commodity:) forControlEvents:UIControlEventTouchUpInside];
                    
                    [scrollView addSubview:clothView];
                    
                    
                    
                }
            }
                
                scrollView.contentSize = CGSizeMake(ScreenWidth, 210*(array_hot2.count/2+array_hot2.count%2)+height+50);
                height += 210*(array_hot2.count/2+array_hot2.count%2);
                labelLoad.text = @"上拉加载更多";
                [activity stopAnimating];
                labelLoad.frame = CGRectMake(0, scrollView.contentSize.height-50, ScreenWidth, 50);

            }else{
                labelLoad.text = @"无更多数据";
                [activity stopAnimating];
            }}
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    
                    [Global showTextHudSub:@"连接失败" view:self.view];
                }
                
            }];
        
        }else{
            
            NSString *requestResultStr=@"没有更多数据了！";
            // [Global showTextHud:requestResultStr view:self.view];
            [Global showTextHudSub:requestResultStr view:self.view];
            
            
        }
    
}
}


-(void)commodity:(UIButton*)sender{
    
    
    [self sea:[NSString stringWithFormat:@"%@",sender.titleLabel.text]];

}

- (void)sea:(NSString *)String{
    //[self.data startAnimating];
    
    
    commdity.caseID=String;
    commdity.gengxin=@"123";
    commdity.gengxinxiangqing=@"";
    CaseDetailViewController*vc1=[[CaseDetailViewController alloc] init];
    
    [self.navigationController pushViewController:vc1 animated:YES];


}






- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return [typeCode count];
   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    ServiceListCell* cell = (ServiceListCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"ServiceListCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    UIButton*jiantou = [[UIButton alloc]init];
    jiantou.frame = CGRectMake(0,0, 8, 13);
    [jiantou setImage:[UIImage imageNamed:@"xiangzuo-.png"] forState:UIControlStateNormal];
    
  
    
    if (tableView == table_list) {
        
        if(commdity.content3.length> 0)
            
        {
            if  ([[typeName objectAtIndex:indexPath.row]isEqualToString:commdity.content2]){
                
                
                
                
                if (![commdity.content3 isEqualToString:@"自定义"]) {
                    cell.detailTextLabel.text =commdity.content3;
                    
                    for (int i = 0; i<[[items objectAtIndex:indexPath.row]count]; i++) {
                        
                        if  ([[[[items objectAtIndex:indexPath.row]objectAtIndex:i]objectForKey:@"itemName"] isEqualToString:commdity.content3]){
                            
                            [current replaceObjectAtIndex:indexPath.row withObject:[[items objectAtIndex:indexPath.row]objectAtIndex:i]];
                            
                        }else{
                        }
                        
                    }
                    
                    
                }else{
                    
                    cell.detailTextLabel.text =[NSString stringWithFormat:@"%@~%@",openTimeTextFied.text,closeTimeTextFied.text];
                    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
                    
                    [dic setValue:[NSString stringWithFormat:@"%@",@"Y"]forKey:@"isCustom"];
                    [dic setValue:[NSString stringWithFormat:@"%@",@"Y"] forKey:@"isRange"];
                    [dic setValue:[NSString stringWithFormat:@"%@",@"自定义"] forKey:@"itemName"];
                    [dic setValue:[NSString stringWithFormat:@"%@",@"other"] forKey:@"itemValue"];
                    [dic setValue:openTimeTextFied.text forKey:@"minValue"];
                    [dic setValue:closeTimeTextFied.text forKey:@"maxValue"];
                    
                    
                    
                    [current replaceObjectAtIndex:indexPath.row withObject:dic];
                    
                    
                    
                }
                
                
                NSLog(@"ffff====%@",typeCode);
                NSLog(@"====%@",isSingle);
                NSLog(@"====%@",current);
                
                
            }
            
            
        }else{
            
            
            cell = [[ServiceListCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] ;
            cell.textLabel.text =[NSString stringWithFormat:@"%@", [typeName objectAtIndex:indexPath.row]];
             UIButton*jiantou = [[UIButton alloc]init];
            jiantou.frame = CGRectMake(ScreenWidth-30,7.5, 30, 30);
            [jiantou setImage:[UIImage imageNamed:@"shouqi"] forState:UIControlStateNormal];
            cell.accessoryView=jiantou;
           cell.detailTextLabel.text = [[congzhicurrent objectAtIndex:indexPath.row]objectForKey:@"itemName"];
            
        }
        
        cell.detailTextLabel.textColor=luseColor;
        if (!saveAccount1) {
            if (typeName.count !=indexPath.row+1) {
                UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(15, 55-0.3, ScreenWidth-30-45, 0.3)];
                lineLabel1.backgroundColor=qianhuicolor;
                [cell.contentView addSubview:lineLabel1];
                
            }
            if (typeName.count ==indexPath.row+1) {
                UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(15, 55-0.3, ScreenWidth-30-45, 0.3)];
                lineLabel1.backgroundColor=qianhuicolor;
                [cell.contentView addSubview:lineLabel1];
                saveAccount1=YES;
                
            }}
        
    }
    
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  55;
}




#pragma mark - Table view delegate


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView == table_list) {
        commdity.content2=typeName[indexPath.row];
        
        
        UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
        [mainScroll removeFromSuperview];
        
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString* Token = [user objectForKey:@"Token"];
        if (Token ==nil) {
            Token=@"dd";
        }
        NSMutableDictionary *param = [NSMutableDictionary new];
        [param setValue:Token forKey:@"Token"];
        
        mainScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(45, 0, ScreenWidth-45, ScreenHeight)];
        mainScroll.delegate = self;
        mainScroll.userInteractionEnabled = YES;
        mainScroll.scrollEnabled = YES;
        mainScroll.backgroundColor=[UIColor whiteColor];
        [self.window addSubview:mainScroll];
        // 手势
        UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyBoad1)];
        singleFingerOne.numberOfTouchesRequired = 1; //手指数
        singleFingerOne.numberOfTapsRequired = 1; //tap次数
        singleFingerOne.delegate = self;
        [mainScroll addGestureRecognizer:singleFingerOne];
        UILabel*title=[[UILabel alloc]initWithFrame:CGRectMake(0, 20, ScreenWidth-50, NavHeight-20)];
        title.text=typeName[indexPath.row];
        title.textAlignment=NSTextAlignmentCenter;
        [mainScroll addSubview:title];
        
        UIButton*quxiao1 = [[UIButton alloc] initWithFrame:CGRectMake(5, 20, 50, NavHeight-20)];
        [quxiao1 setTitle:@"取消" forState:UIControlStateNormal];
        [quxiao1 setTitleColor:zhonghuiColor forState:UIControlStateNormal];
        quxiao1.titleLabel.font=[UIFont systemFontOfSize:16];
        [quxiao1 addTarget:self action:@selector(quxiao1:) forControlEvents:UIControlEventTouchUpInside];
        [mainScroll addSubview:quxiao1];
        UILabel*line=[[UILabel alloc]initWithFrame:CGRectMake(0, NavHeight-0.35, ScreenWidth-45, 0.35)];
        line.backgroundColor=qianhuicolor;
        [mainScroll addSubview:line];
        
        
        arrrss=[[NSMutableArray alloc]init];
        
        arrrss=[[items objectAtIndex:indexPath.row] mutableCopy];
        
        NSLog(@"%@",arrrss);
        if (arrrss.count>0) {
            
            for (int i = 0; i<arrrss.count; i++) {
                
                daogouView = [[sxview alloc]initWithFrame:CGRectMake(15, NavHeight+(55*i), ScreenWidth-45, 55)];
                
                daogouView.buttonView.titleLabel.textAlignment=NSTextAlignmentRight;
                daogouView.buttonView.titleLabel.font=[UIFont systemFontOfSize:16];
                daogouView.buttonView.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
                [daogouView.buttonView setTitle: [[[items objectAtIndex:indexPath.row] objectAtIndex:i] objectForKey:@"itemName"] forState:UIControlStateNormal];
                daogouView.buttonView.backgroundColor=[UIColor whiteColor];
                daogouView.buttonView.tag=i;
                [daogouView.buttonView setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
                [daogouView.buttonView setTitleColor:qianheicolor forState:0];

                
                
                
                [mainScroll addSubview:daogouView];
                
                if ([[[[items objectAtIndex:indexPath.row] objectAtIndex:i] objectForKey:@"itemName"] isEqualToString:@"自定义"]) {
                    
                    if ([typeName[indexPath.row] isEqualToString:@"面积"]) {
                        
                        [daogouView.buttonView setTitleColor:luseColor forState:0];
                        
                        UIButton*login = [[UIButton alloc] initWithFrame:CGRectMake(0, ScreenHeight-48, ScreenWidth-45, 48)];
                        [login setTitle:@"确认" forState:UIControlStateNormal];
                        [login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                        login.backgroundColor=luseColor;
                        [login addTarget:self action:@selector(queren:) forControlEvents:UIControlEventTouchUpInside];
                        [mainScroll addSubview:login];
                        
                        
                        
                        
                        _da = [[UIDatePicker alloc] init];
                        [_da addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
                        _da.datePickerMode = UIDatePickerModeDate;
                        _da.backgroundColor=[UIColor whiteColor];
                        
                        
                        openTimeTextFied=[[UITextField alloc]initWithFrame:CGRectMake(50,  daogouView.bottom-55, 110,  55)];
                        openTimeTextFied.borderStyle = UITextBorderStyleNone;
                        openTimeTextFied.font = [UIFont systemFontOfSize: contenfot];
                        openTimeTextFied.textColor=[UIColor blackColor];
                        openTimeTextFied.autocapitalizationType = UITextAutocapitalizationTypeSentences;
                        openTimeTextFied.tag=2;
                        openTimeTextFied.placeholder = @"最小取值"; //默认显示的字
                        openTimeTextFied.delegate = self;
                        openTimeTextFied.text=commdity.opentime;
                        openTimeTextFied.textAlignment=NSTextAlignmentCenter;
                        [openTimeTextFied setUserInteractionEnabled:YES];
                        //                                   openTimeTextFied.clearButtonMode = UITextFieldViewModeWhileEditing;
                        [mainScroll addSubview:openTimeTextFied];
                        
                        UILabel* fuhao=[[UILabel alloc]initWithFrame:CGRectMake(openTimeTextFied.right, daogouView.bottom-55,15,55)];
                        fuhao.text=[NSString stringWithFormat:@"%@", @"～"];
                        fuhao.font=[UIFont systemFontOfSize:contenfot];
                        fuhao.textColor=[UIColor blackColor];
                        [mainScroll addSubview:fuhao];
                        
                        closeTimeTextFied=[[UITextField alloc]initWithFrame:CGRectMake(fuhao.right-5,  daogouView.bottom-55, 110,  55)];
                        closeTimeTextFied.borderStyle = UITextBorderStyleNone;
                        closeTimeTextFied.font = [UIFont systemFontOfSize: contenfot];
                        closeTimeTextFied.textColor=[UIColor blackColor];
                        closeTimeTextFied.autocapitalizationType = UITextAutocapitalizationTypeSentences;
                        closeTimeTextFied.tag=3;
                        closeTimeTextFied.placeholder = @"最大取值"; //默认显示的字
                        closeTimeTextFied.textAlignment=NSTextAlignmentCenter;
                        closeTimeTextFied.text= commdity.closeTime;
                        closeTimeTextFied.delegate = self;
                        [closeTimeTextFied setUserInteractionEnabled:YES];
                        //                                   closeTimeTextFied.clearButtonMode = UITextFieldViewModeWhileEditing;
                        [mainScroll addSubview:closeTimeTextFied];
                        //                                   [_da setAccessibilityLanguage:@"Chinese"];
                        
//                        openTimeTextFied.inputView = _da;
//                        closeTimeTextFied.inputView = _da;
                        
                    }
                    
                    
                }else{
                    [daogouView.buttonView addTarget:self action:@selector(daogouView:) forControlEvents:UIControlEventTouchUpInside];
                }
                
                if ([[[[items objectAtIndex:indexPath.row] objectAtIndex:i] objectForKey:@"itemName"] isEqualToString:cell.detailTextLabel.text]) {
                    
                    UIImageView*image=[[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth-50-60, 16.5, 18, 18)];
                    image.image=[UIImage imageNamed:@"huiyuan_gou"];
                    
                    [daogouView addSubview:image];
                    
                }
            }
            
            
            
            
            [mainScroll setContentSize:CGSizeMake(ScreenWidth-45, daogouView.bottom)];
        }
        
        
        
        
    }
        
    
}

- (void)dateChanged:(id)sender
{
    
    UIDatePicker* picker = (UIDatePicker*)sender;
    NSDateFormatter* df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd"];
    
    
    
    
    if ([openTimeTextFied isFirstResponder])
    {
        openTimeTextFied.text = [NSString stringWithFormat:@"%@", [df stringFromDate:picker.date]];
        
        
        
    }else{
        
        closeTimeTextFied.text = [NSString stringWithFormat:@"%@", [df stringFromDate:picker.date]];
    }
    
    
}

-(void)quxiao1:(UIButton*)sender{
    [openTimeTextFied resignFirstResponder];
    [closeTimeTextFied resignFirstResponder];
    [mainScroll removeFromSuperview];
    
}
-(void)queren:(UIButton*)sender{
    
    if ([ openTimeTextFied.text length] < 1)
    {
        [self showAlertMessage:@"请选择开始时间"];
    }
    else
    {
        
        if ([closeTimeTextFied.text length] <1)
        {
            [self showAlertMessage:@"请选择结束时间"];
        }
        else{
            
            commdity.opentime=openTimeTextFied.text;
            commdity.closeTime=closeTimeTextFied.text;
            [mainScroll removeFromSuperview];
            commdity.content3=@"自定义";
            [table_list reloadData];
            
            
        }}
    
    
}

-(void)daogouView:(UIButton*)sender{
    commdity.opentime=nil;
    commdity.closeTime=nil;
    commdity.content3=sender.titleLabel.text;
    [mainScroll removeFromSuperview];
    [table_list reloadData];
    
    
    
}


-(void)sxuan:(UIButton*)sender{
    
    
    if(commdity.content3.length> 0){
        
        
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, ScreenHeight)];
        view.backgroundColor=[UIColor blackColor];
        view.alpha=0.5;
        [self.window makeKeyAndVisible];
        [self.window addSubview:view];
        
        
        UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyBoad)];
        singleFingerOne.numberOfTouchesRequired = 1; //手指数
        singleFingerOne.numberOfTapsRequired = 1; //tap次数
        singleFingerOne.delegate = self;
        [view addGestureRecognizer:singleFingerOne];
        
        table_list.frame=CGRectMake(45, 0, ScreenWidth-45, ScreenHeight);
        [self.window addSubview:table_list];
        UIButton*login = [[UIButton alloc] initWithFrame:CGRectMake(45, ScreenHeight-48, ScreenWidth-45, 48)];
        [login setTitle:@"确认" forState:UIControlStateNormal];
        [login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        login.backgroundColor=luseColor;
        [login addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
        [self.window addSubview:login];
        
        
    }else{
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, ScreenHeight)];
        view.backgroundColor=[UIColor blackColor];
        view.alpha=0.5;
        [self.window makeKeyAndVisible];
        [self.window addSubview:view];
        
        
        UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyBoad)];
        singleFingerOne.numberOfTouchesRequired = 1; //手指数
        singleFingerOne.numberOfTapsRequired = 1; //tap次数
        singleFingerOne.delegate = self;
        [view addGestureRecognizer:singleFingerOne];
        
        
         saveAccount1=NO;
        table_list = [[UITableView alloc]initWithFrame:CGRectMake(45, 0, ScreenWidth-45, ScreenHeight) style:UITableViewStylePlain];

        table_list.delegate = self;
        table_list.dataSource = self;
        table_list.rowHeight = 60;
        table_list.separatorStyle=UITableViewCellSeparatorStyleNone;
        [self.window addSubview:table_list];
        
        
        UIView*Header=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth-50, NavHeight)];
        Header.backgroundColor=[UIColor whiteColor];
        table_list.tableHeaderView=Header;
        UILabel*line=[[UILabel alloc]initWithFrame:CGRectMake(0, NavHeight, ScreenWidth-50, 0.35)];
        line.backgroundColor=qianhuicolor;
        [Header addSubview:line];
        
        UILabel*title=[[UILabel alloc]initWithFrame:CGRectMake(0, 20, ScreenWidth-50, NavHeight-20)];
        title.text=@"筛选";
        title.textAlignment=NSTextAlignmentCenter;
        [Header addSubview:title];
        
        UIButton*quxiao = [[UIButton alloc] initWithFrame:CGRectMake(5, 20, 50, NavHeight-20)];
        [quxiao setTitle:@"取消" forState:UIControlStateNormal];
        [quxiao setTitleColor:zhonghuiColor forState:UIControlStateNormal];
        quxiao.titleLabel.font=[UIFont systemFontOfSize:16];
        [quxiao addTarget:self action:@selector(quxiao:) forControlEvents:UIControlEventTouchUpInside];
        [Header addSubview:quxiao];
        
        UIButton*chongzhi = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth-50-60, 20, 50, NavHeight-20)];
        [chongzhi setTitle:@"重置" forState:UIControlStateNormal];
        [chongzhi setTitleColor:luseColor forState:UIControlStateNormal];
        chongzhi.titleLabel.font=[UIFont systemFontOfSize:16];
        [chongzhi addTarget:self action:@selector(chongzhi:) forControlEvents:UIControlEventTouchUpInside];
        [Header addSubview:chongzhi];
        
        UIButton*login = [[UIButton alloc] initWithFrame:CGRectMake(45, ScreenHeight-48, ScreenWidth-45, 48)];
        [login setTitle:@"确认" forState:UIControlStateNormal];
        [login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        login.backgroundColor=luseColor;
        [login addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
        [self.window addSubview:login];
    }
}

-(void)chongzhi:(UIButton*)sender{
    [shaixuanBtn setTitleColor:shenhuicolor forState:UIControlStateNormal];
    [sxBtn setImage:[UIImage imageNamed:@"shaixuan"] forState:UIControlStateNormal];
    commdity.content3=@"";
    current= [congzhicurrent mutableCopy];
    saveAccount1=NO;
    NSLog(@"----------%@",current);
    [table_list reloadData];
    
}

-(void)quxiao:(UIButton*)sender{
    
    
    [self.window makeKeyWindow ];
    self.window.hidden=YES;
    table_list.frame=CGRectMake(ScreenWidth, 0, ScreenWidth-45, ScreenHeight);
    
    
}

-(void)login:(UIButton*)sender{
    
    [self.window makeKeyWindow ];
    self.window.hidden=YES;
    table_list.frame=CGRectMake(ScreenWidth, 0, ScreenWidth-45, ScreenHeight);
    
    searchBeans=[NSMutableArray array];
    
    for (int i=0; i<current.count; i++) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        NSString *typeCode1 = [typeCode objectAtIndex:i];
        NSString *isSingle1 = [isSingle objectAtIndex:i];
        NSString *typeName1 = [typeName objectAtIndex:i];
        NSString *dataType1 = [dataType objectAtIndex:i];
        NSMutableArray* currentarray=[NSMutableArray array];
        [currentarray insertObject:[current objectAtIndex:i] atIndex:0];
        [dic setValue:[NSString stringWithFormat:@"%@",typeCode1]forKey:@"typeCode"];
        [dic setValue:[NSString stringWithFormat:@"%@",isSingle1] forKey:@"isSingle"];
        [dic setValue:[NSString stringWithFormat:@"%@",typeName1] forKey:@"typeName"];
        [dic setValue:[NSString stringWithFormat:@"%@",dataType1] forKey:@"dataType"];
        [dic setValue:currentarray forKey:@"items"];
        [searchBeans addObject:dic];
    }
    
    NSLog(@"====%@",searchBeans);
    
    if ([commdity.content3 isEqualToString:@""]) {
        searchBeans=nil;
    }
    [self setup];
    
    
}
-(void)closeKeyBoad1{
    [openTimeTextFied resignFirstResponder];
    [closeTimeTextFied resignFirstResponder];
}
-(void)closeKeyBoad{
    [openTimeTextFied resignFirstResponder];
    [closeTimeTextFied resignFirstResponder];
    [self.window makeKeyWindow ];
    self.window.hidden=YES;
    
    table_list.frame=CGRectMake(ScreenWidth, 0, ScreenWidth-45, ScreenHeight);
    
   
}



- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}






-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    [segue.destinationViewController setHidesBottomBarWhenPushed:YES];
}
@end
